import ecActions from 'actions/exchange'
import { saveData } from 'utils'
import { message } from 'antd'

export default {
  state: {
  }, // initial state
  reducers: {
    save (state, payload) {
      return saveData(state, payload)
    }
  },
  effects: dispatch => ({
    // 获取兑换商品列表
    async getEcGoodsList (params) {
      this.save({
        ecGoods: null
      })
      const res = await ecActions.getEcGoodsList(params)
      if (res.success) {
        this.save({
          ecGoods: {
            list: res.data.exchangeCommodityListNodes,
            total: res.data.total
          }
        })
      } else {
        message.error(res.msg)
      }
    },
    // 获取兑换商品详情
    async getEcGoodsDetails (params) {
      this.save({
        ecGoodsDetails: null
      })
      const res = await ecActions.getEcGoodsDetails(params)
      if (res.success) {
        const images = res.data.images
        let coverImg; let imageList = []
        images.forEach((element, index) => {
          if (element.cover === 1) {
            coverImg = element
            images.splice(index, 1)
          }
        })
        imageList = images
        this.save({
          ecGoodsDetails: {
            coverImg,
            imageList
          }
        })
      }
    },
    // 添加兑换商品
    async addEcGoods (params) {
      this.save({
        addEcGoodsRequest: null
      })
      const res = await ecActions.addEcGoods(params)
      if (res.success) {
        this.save({
          addEcGoodsRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 编辑兑换商品
    async editEcGoods (params) {
      this.save({
        editEcGoodsRequest: null
      })
      const res = await ecActions.editEcGoods(params)
      if (res.success) {
        this.save({
          editEcGoodsRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 删除兑换商品
    async delEcGoods (params) {
      this.save({
        delEcGoodsRequest: null
      })
      const res = await ecActions.delEcGoods(params)
      if (res.success) {
        this.save({
          delEcGoodsRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 兑换商品上下架
    async updateEcGoods (params) {
      this.save({
        updateEcGoodsRequest: null
      })
      const res = await ecActions.updateEcGoods(params)
      if (res.success) {
        this.save({
          updateEcGoodsRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 删除兑换订单
    async delEcGoodsOrder (params) {
      this.save({
        delEcGoodsRequest: null
      })
      const res = await ecActions.delEcGoodsOrder(params)
      if (res.success) {
        this.save({
          delEcGoodsRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 取消兑换订单
    async cancelEcGoodsOrder (params) {
      this.save({
        cancelEcGoodsRequest: null
      })
      const res = await ecActions.cancelEcGoodsOrder(params)
      if (res.success) {
        this.save({
          cancelEcGoodsRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 发货
    async shipEcGoods (params) {
      this.save({
        sendEcGoodsRequest: null
      })
      const res = await ecActions.shipEcGoods(params)
      if (res.success) {
        this.save({
          sendEcGoodsRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 获取兑换订单列表
    async getEcGoodsOrder (params) {
      this.save({
        ecOrder: null
      })
      const res = await ecActions.getEcGoodsOrder(params)
      if (res.success) {
        this.save({
          ecOrder: {
            list: res.data.exchangeOrderNodes,
            total: res.data.total
          }
        })
      } else {
        message.error(res.msg)
      }
    }
  })
}
